<!--
 * @Date: 2025-10-02 13:26:31
 * @LastEditors: 贾二小 erxiao.jia@outlook.com
 * @LastEditTime: 2025-10-02 15:17:12
 * @FilePath: /admin-vue/src/components/ad/ad-upload.vue
-->
<script setup lang="ts">
import type { UploadCustomRequestOptions, UploadFileInfo } from 'naive-ui';

const fileList = ref<UploadFileInfo[]>([
   
  // {
  //   id: 'a',
  //   name: '我是自带url的图片.png',
  //   status: 'finished',
  //   url: 'https://07akioni.oss-cn-beijing.aliyuncs.com/07akioni.jpeg'
  // }
])

const { avatar } = useUpload()

const customRequest = async ({
  file,
  data,
  headers,
  withCredentials,
  action,
  onFinish,
  onError,
  onProgress
}: UploadCustomRequestOptions) =>{
  const formData = new FormData()
  if (data) {
    Object.keys(data).forEach((key) => {
      formData.append(
        key,
        data[key as keyof UploadCustomRequestOptions['data']]
      )
    })
  }
  formData.append('file', file.file as File)
  //文件上传
  const {data: upload} = await avatar(formData);

  console.log(upload);
  
  onFinish()
}


</script>

<template>
  <n-upload :default-file-list="fileList"
    list-type="image-card"
    :custom-request="customRequest"
     />
</template>